//
//  sqlManager.m
//  sqllite
//
//  Created by 梅杰 on 16/7/30.
//  Copyright © 2016年 梅杰. All rights reserved.
//

#import "sqlManager.h"
#import "sqlite3.h"

#define KNameFile (@"Student.sqlite")

@interface sqlManager ()
{
    sqlite3 *db;
}
@end

@implementation sqlManager

static sqlManager *manager = nil;

+ (instancetype)shareManager{
    
    return [[self alloc] init];
}
//多线程安全
+ (instancetype) allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [super allocWithZone:zone];
        [manager createDataBaseTableIfNeeded];
    });
    return manager;
}

- (NSString *) applicationDocumentsDirectoryFile{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [path firstObject];
    NSString *filePath = [documentDirectory stringByAppendingPathComponent:KNameFile];
    return filePath;
}
- (void) createDataBaseTableIfNeeded{
    NSString *writetablePath = [self applicationDocumentsDirectoryFile];
    NSLog(@"数据库的地址是 ： %@",writetablePath);
    
    if (sqlite3_open([writetablePath UTF8String], &db) != SQLITE_OK) {
        NSLog(@"数据库打开失败");
        sqlite3_close(db);
        NSAssert(NO, @"断言数据库打开失败");
    }else{
        char *err;
        NSString *createSQL = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS StudentName (name TEXT PRIMARY KEY , sex TEXT);"];
        
        //执行语句
        if (sqlite3_exec(db, [createSQL UTF8String], NULL, NULL, &err) !=SQLITE_OK){
            sqlite3_close(db);
            NSLog(@"sql语句执行建表失败");
            NSAssert(NO, @"sql语句执行建表失败");
        }
        sqlite3_close(db);
    };
}

//- (Student *)searchByName:(Student *)model{
//    NSString *path = [self applicationDocumentsDirectoryFile];
//    if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) {
//        sqlite3_close(db);
//        NSAssert(NO, @"打开数据库失败");
//    }else{
//        NSString *sql = @"SELECT name,sex FROM StudentName where name = ?";
//        
//        sqlite3_stmt *statement;//语句对象
//        
//       int flag = sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, NULL);
//        if (flag != SQLITE_OK) {
//            NSAssert1(0, @"error:%s", sqlite3_errmsg(db));
//            sqlite3_finalize(statement);
//            sqlite3_close(db);
//            
//        }else{
////            NSAssert(NO, @"查询成功");
//            NSString *name = model.name;
//            sqlite3_bind_text(statement, 1, [name UTF8String], -1, NULL);
//            
//            if(SQLITE_ROW == sqlite3_step(statement)){
//                char *name = (char *) sqlite3_column_text(statement, 0);
//                char *sex = (char *) sqlite3_column_text(statement, 1);
//                
//                NSString *nameString = [[NSString alloc] initWithUTF8String:name];
//                NSString *sexString = [[NSString alloc] initWithUTF8String:sex];
//                
//                sqlite3_finalize(statement);
//                sqlite3_close(db);
//                
//                Student *aStudent = [[Student alloc] init];
//                aStudent.name = nameString;
//                aStudent.sex = sexString;
//                
//                return aStudent;
//            };
//        }
//    }
//    
//    return nil;
//}
//
//- (int)insert:(Student *)model{
//    NSString *path = [self applicationDocumentsDirectoryFile];
//    
//    if (sqlite3_open([path UTF8String], &db) != SQLITE_OK) {
//        NSAssert(NO, @"数据库打开失败");
//        sqlite3_close(db);
//    }else{
//        NSString *sql = @"INSERT OR REPLACE INTO StudentName (name , sex) VALUES (?,?)";
//        sqlite3_stmt *statement;
//        
//        if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
//            sqlite3_bind_text(statement, 1, [model.name UTF8String], -1, NULL);
//            sqlite3_bind_text(statement, 2, [model.sex UTF8String], -1, NULL);
//            
//            if (sqlite3_step(statement) != SQLITE_DONE) {
//                NSAssert(NO, @"插入失败");
//                
//                sqlite3_finalize(statement);
//                sqlite3_close(db);
//            }
//        }else{
//            NSAssert1(0,@"Error:%s",sqlite3_errmsg(db));
//            sqlite3_finalize(statement);
//            sqlite3_close(db);
//        }
//        
//    }
//    return 0;
//}
@end
